package com.sh.data.engine.repository.dao.api.entity;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.sh.data.engine.repository.dao.base.EngineBaseModel;
import lombok.*;

import java.util.Date;
import java.util.List;

/**
 * @author : fuchen
 * @date : 2022/1/19 16:44
 */
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
@TableName(value = "tb_data_api", autoResultMap = true)
@EqualsAndHashCode(callSuper = true)
public class DataApiEntity extends EngineBaseModel<DataApiEntity> {

    private String apiMethod;

    private String apiName;

    private String apiProtocol;

    private String apiPath;

    private Integer apiStatus;

    private Long apiGroupId;

    private String apiDesc;

    private Integer pageable;

    private Integer status;

    private Long totalCount;

    private Long successCount;

    private Long failCount;

    private Long projectId;

    private Integer apiType;

    private String responseExample;

    private Date publishTime;

    private Long datasourceId;

    private String tableName;

    // 0-apiToken 1-userToken
    private Integer authType;

    // 脚本模式
    private String apiScript;

    // 1 出入参加密 0
    private Integer isSecurity;

    // 指标
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private DataApiIndexEntity dataApiIndex;

    // 参数
    @TableField(exist = false)
    private List<DataApiParamEntity> apiParamEntityList;

    private String apiParamEntity;

    public List<DataApiParamEntity> getApiParamEntityList() {
        return JSONArray.parseArray(apiParamEntity, DataApiParamEntity.class);
    }

    public void setApiParamEntity() {
        this.apiParamEntity = JSONArray.toJSONString(apiParamEntityList);
    }

    // 向导模式
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private DataApiRegisterEntity dataApiRegister;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class DataApiIndexEntity {

        private Long dirId;

        private Long indexId;
    }

    @Data
    public static class DataApiParamEntity {

        private Integer paramType;

        private String paramName;

        private String paramPosition;

        private String bindColumnName;

        private String paramDataType;

        private Integer paramOperator;

        private Integer paramRequire;

        private String exampleValue;

        private String defaultValue;

        private String paramDesc;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class DataApiRegisterEntity {

        private String httpRequestMethod;

        // http url
        private String url;
    }
}
